package restPROC;


import java.sql.SQLException;










import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import dao.daoKonekcija;
import dao.daoPermisije;
import dao.daoStanica;
import utilClasses.ListaStanica;


@Path("/stanice") 
public class Stanica {
	@GET
	@Path("/unos")
	@Produces("text/plain")
	public String unesiStanicu(@QueryParam("gduzina") String gduzina, @QueryParam("gsirina") String gsirina,@QueryParam("naziv") String naziv,@QueryParam("redni") String redni,@QueryParam("idlinija") String linija,@Context HttpServletRequest req)
	{
		HttpSession sess = req.getSession(true);
		String reqkorisnik=sess.getAttribute("korisnik").toString();
		System.out.println(reqkorisnik);
		try {
			boolean jelima=daoPermisije.provjeri(reqkorisnik, "modstanice");
			System.out.println(jelima);
			if(!jelima) return null;
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		int k=-1;
		try {
			
			k=daoStanica.kreirajStanicu(Double.parseDouble(gduzina), Double.parseDouble(gsirina), naziv, Integer.parseInt(redni),Integer.parseInt(linija));
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return Integer.toString(k);
	}

	@GET
	@Path("/pomjeri")
	@Produces("text/plain")
	public String pomjeri(@QueryParam("id") String id, @QueryParam("GSirina") String lat,@QueryParam("GDuzina") String lng,@Context HttpServletRequest req)
	{
		HttpSession sess = req.getSession(true);
		String reqkorisnik=sess.getAttribute("korisnik").toString();
		System.out.println(reqkorisnik);
		try {
			boolean jelima=daoPermisije.provjeri(reqkorisnik, "modstanice");
			System.out.println(jelima);
			if(!jelima) return null;
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String rez="false";
		try {
			rez=	daoStanica.pomjeriStanicu(Integer.parseInt(id),Double.parseDouble(lat),Double.parseDouble(lng));
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rez;
	}
	@GET
	@Path("/listastanica")
	@Produces({"application/xml"})
	public ListaStanica getStanice(@QueryParam("id") String id)
	{
		ListaStanica rez = new ListaStanica();
		try {
			
			rez=daoStanica.dajStanice(Integer.parseInt(id));
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rez;
		
	}
	
	
	@POST
	@Path("/brisanjestanica")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces("text/plain")
	public String deleteStanica(@FormParam("naziv") String naziv,@Context HttpServletRequest req)
	{
		HttpSession sess = req.getSession(true);
		String reqkorisnik=sess.getAttribute("korisnik").toString();
		System.out.println(reqkorisnik);
		try {
			boolean jelima=daoPermisije.provjeri(reqkorisnik, "modstanice");
			System.out.println(jelima);
			if(!jelima) return null;
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			
			//System.out.print(naziv);
			daoStanica.brisiStanicu(naziv);
			
			
			return "true";
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "false";
	}
	
	
	@GET
	@Path("/dajImena")
	@Produces({"application/xml"})
	public ListaStanica dajImena()
	{
		ListaStanica rez=new ListaStanica();
		try {
			
			rez=daoStanica.dajImenaStanica();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return rez;
	}

}